package com.ruoyi.web.controller.order.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.order.pojo.*;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.http.parser.MediaType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 【请填写功能名称】Controller
 *
 * @author 1029432816@qq.com
 * @date 2020-02-25
 */
@Controller
@RequestMapping("/order")
@Api("订单信息管理")
public class OrderController extends BaseController {

    private String prefix = "system/order";

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ISysDictDataService dictDataService;

    @RequiresPermissions("system:order:searchView")
    @GetMapping("/searchView")
    public String searchView() {
        return prefix + "/search";
    }

    @RequiresPermissions("system:order:addView")
    @GetMapping("/addView")
    public String addView() {
        return prefix + "/add";
    }

    @RequiresPermissions("system:order:reportView")
    @GetMapping("/reportView")
    public String reportView() {
        return prefix + "/report";
    }

    @RequiresPermissions("system:order:detailView")
    @GetMapping("/detailView")
    public String detailView(Long id, ModelMap mmap) {
        OrderVO order = orderService.getOrderVOById(id);
        order.setOrderId(order.getId().toString());
        mmap.put("order", order);
        return prefix + "/detail";
    }

    /**
     * 查询【订单】列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询订单")
    public TableDataInfo list(@RequestBody OrderListDTO orderListDTO) {
        List<OrderVO> list = orderService.list(orderListDTO);
        return getDataTable(list);
    }

    /**
     * 新增保存【订单】
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "【添加订单】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("添加订单")
    public AjaxResult addSave(@RequestBody OrderDTO orderDTO) throws ParseException {
        orderDTO.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(orderService.insertOrder(orderDTO));
    }

    @GetMapping("/getAllStatisticsCategory")
    @ResponseBody
    @ApiOperation("查询所有的统计类别")
    public AjaxResult getAllStatisticsCategory() {
        return AjaxResult.success(dictDataService.getAllProjectCategory("handle_project_categories"));
    }

    @RequiresPermissions("system:order:getById")
    @GetMapping("/getById")
    @ApiOperation("根据id查询订单数据")
    @ResponseBody
    public AjaxResult getById(Long id) {
        return AjaxResult.success(orderService.getOrderVOById(id));
    }

    @RequiresPermissions("system:order:getDisinfectionStatisticsReport")
    @GetMapping("/getDisinfectionStatisticsReport")
    @ApiOperation("根据条件导出业务报表")
    public void getDisinfectionStatisticsReport(DisinfectionReportDTO disinfectionReportDTO, HttpServletResponse response) throws Exception {
        orderService.getDisinfectionStatisticsReport(disinfectionReportDTO, response);
    }

    @GetMapping("/getLatestOrder")
    @ApiOperation("获取最新的订单记录")
    @ResponseBody
    public AjaxResult getLatestOrder() {
        return AjaxResult.success(orderService.getLatestOrder());
    }

    @RequiresPermissions("system:order:getBusinessVolumeReport")
    @GetMapping(value = "/getBusinessVolumeReport", produces = "application/vnd.ms-excel")
    @ApiOperation("获取业务量上报表")
    public void getBusinessVolumeReport(BusinessVolumeReportDTO businessVolumeReportDTO, HttpServletResponse response) throws Exception {
        businessVolumeReportDTO.setCreateBy(ShiroUtils.getLoginName());
        orderService.getBusinessVolumeReport(businessVolumeReportDTO, response);
    }

    @RequiresPermissions("system:order:getDoseReport")
    @GetMapping("/getDoseReport")
    @ApiOperation("获取用药量上报表")
    public void getDoseReport(BusinessVolumeReportDTO businessVolumeReportDTO, HttpServletResponse response) throws IOException {
        businessVolumeReportDTO.setCreateBy(ShiroUtils.getLoginName());
        orderService.getDoseReport(businessVolumeReportDTO, response);
    }

    @RequiresPermissions("system:order:delete")
    @PostMapping("/deleteBatchByIds")
    @ApiOperation("通过id集合批量删除")
    @Log(title = "【删除】", businessType = BusinessType.DELETE)
    @ResponseBody
    public AjaxResult deleteBatchByIds(@RequestBody List<Long> ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 修改【订单】
     */
    @RequiresPermissions("system:order:editView")
    @GetMapping("/editView")
    public String editView(Long id, ModelMap mmap) {
        OrderVO order = orderService.getOrderVOById(id);
        order.setOrderId(order.getId().toString());
        mmap.put("order", order);
        return prefix + "/edit";
    }

    /**
     * 修改【订单】
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "【修改订单】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation("通过id修改订单")
    public AjaxResult edit(@RequestBody OrderDTO order) {
        order.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(orderService.updateOrder(order));
    }

    @RequiresPermissions("system:order:getTrafficChargeReport")
    @GetMapping("/getTrafficChargeReport")
    @ApiOperation("获取业务量收费上报表")
    public void getTrafficChargeReport(BusinessVolumeReportDTO businessVolumeReportDTO, HttpServletResponse response) throws IOException {
        businessVolumeReportDTO.setCreateBy(ShiroUtils.getLoginName());
        orderService.getTrafficChargeReport(businessVolumeReportDTO, response);
    }

}
